package club.calong.sanniu.service;

import club.calong.sanniu.http.response.ListResponseData;
import club.calong.sanniu.pojo.Permission;
import club.calong.sanniu.repository.PermissionRepository;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PermissionService {

	private final PermissionRepository repository;

	public PermissionService(PermissionRepository repository) {
		this.repository = repository;
	}

	public boolean insert(Permission permission) {

		return repository.insert(permission) == 1;
	}

	public boolean delete(Integer id) {

		return repository.deleteById(id) == 1;
	}

	public boolean update(Permission permission) {

		return repository.updateById(permission) == 1;
	}

	public Permission select(int id) {

		return repository.selectById(id);
	}

	public ListResponseData<Permission> list(Integer page, Integer size) {
		if (page != null && size != null) {
			QueryWrapper<Permission> wrapper = new QueryWrapper<>();
			wrapper.orderByAsc(Permission.NAME);
			Page<Permission> rolePage = repository.selectPage(new Page<>(page, size), wrapper);
			return new ListResponseData<>(rolePage.getRecords(), rolePage.getTotal());
		}else {
			List<Permission> permissions = repository.selectList(null);
			return new ListResponseData<>(permissions, (long) permissions.size());
		}
	}
}
